package grafico;

import javax.swing.*;

import voxToolkit.VoxButton;

import audio.GerenteAudio;

import main.TocadorDaisy;

import java.awt.event.*;

public class ControleEventos implements ActionListener, KeyListener, 
										FocusListener, MouseListener, 
										WindowListener
{
	static private ControleEventos instancia = null;
	
	private Action spaceAction;
	
	private ControleEventos()
	{
		spaceAction = new AbstractAction() 
		{
			static final long serialVersionUID = 1;

		    public void actionPerformed(ActionEvent e) 
		    {
				TocadorDaisy controle = TocadorDaisy.instancia();

				controle.reverteEstadoParagrafo();
		    }
		};
	}
	
	static public ControleEventos instancia()
	{
		if ( instancia == null )
			instancia = new ControleEventos();
		
		return instancia;
	}
	
	public void acaoBotao( VoxButton btn ) 
	{
		TocadorDaisy controle = TocadorDaisy.instancia();

		if ( btn.getNome().equals("abrir") )
		{
			btn.setBotaoAtivado();
			controle.abrirLivro(); 
		}
		else if ( btn.getNome().equals("play")||
				  btn.getNome().equals("stop")  )
		{
			btn.setBotaoAtivado();
			btn.reverte();
			controle.reverteEstadoContinuo();
		}
		else if ( btn.getNome().equals("avanca") )
		{
			btn.setBotaoAtivado();
			controle.avancaSentenca();
		}
		else if ( btn.getNome().equals("retrocede") )
		{
			btn.setBotaoAtivado();
			controle.recuaSentenca();
		}
		else if ( btn.getNome().equals("listar") )
		{
			btn.setBotaoAtivado();
			controle.mostrarEstrutura();
		}
		else if ( btn.getNome().equals("configSom") )
		{
			btn.setBotaoAtivado();
			DlgConfigAudio dlg = new DlgConfigAudio();
			dlg.setVisible(true);
		}
		else if ( btn.getNome().equals("config") )
		{
			btn.setBotaoAtivado();
			DlgConfigs dlg = new DlgConfigs();
			dlg.setVisible(true);
		}
		else if ( btn.getNome().equals("sem_som") ||
				  btn.getNome().equals("com_som") ||
				  btn.getNome().equals("com_texto") ||
				  btn.getNome().equals("sem_texto")  )
		{
			btn.reverte();
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if ( e.getSource() instanceof VoxButton )
		{
			acaoBotao( (VoxButton)e.getSource() );
		}
	}
	
	public Action getSpaceAction()
	{
		return spaceAction;
	}
	
	@Override
    public void keyTyped(KeyEvent e) 
    {
		
		if ( e.getSource() instanceof VoxButton )
		{
			if (e.getKeyChar() == KeyEvent.VK_ENTER)
				acaoBotao( (VoxButton)e.getSource() );
		}
    }

	@Override
    public void keyPressed(KeyEvent e) 
    {
		
    }

	@Override
    public void keyReleased(KeyEvent e) 
    {
    }

	@Override
	public void focusGained(FocusEvent e) 
	{
		if ( e.getSource() instanceof VoxButton )
		{
			try
			{
				VoxButton btn = (VoxButton)e.getSource();
				
				btn.tocarLabelGravado();
				btn.setBorder(Fabrica.bordaClara);
			}
			catch (Exception e1)
			{
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
	
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered( MouseEvent e )  {
	
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) 
	{
		GerenteAudio.instancia().encerrarAudio();
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
